package game.adm.dao.impl;

import game.adm.dao.PayDAO;
import game.adm.dao.dto.PayDTO;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import projectx.core.base.ProjectXSystemException;
import projectx.core.dao.AbstractDAO;
import projectx.core.dao.DataAccessException;
import projectx.core.dao.ExceptionHandler;
import projectx.core.dao.impl.PreparedStatementBuilder;

public class PayDAOImpl extends AbstractDAO implements PayDAO {

	public enum field {
		secretkey,
		appid,
		requesttoken,
		body,
		notifyurl,
		returnurl,
		ip
	}
	public PayDAOImpl() throws ProjectXSystemException {
		super();
	}

	@Override
	public PayDTO getPay() throws DataAccessException{
		ResultSet rs = null;
		PreparedStatement ps = null; 
		String sql = null;
		try {
			_builder = (PreparedStatementBuilder) SqlLoader.getBuilders().get(
					"ADM_PayDAO_SELECT");
			sql = _builder.getSql(null, null);
			ps = _builder.createStatement(_conn, sql, false, false);
			debugSql();
			
			rs = ps.executeQuery();

			if (rs.next()) {
				String secretkey = getString(rs.getString(field.secretkey.ordinal()+1));
				String appid = getString(rs.getString(field.appid.ordinal()+1));
				String requesttoken = getString(rs.getString(field.requesttoken.ordinal()+1));
				String body = getString(rs.getString(field.body.ordinal()+1));
				String notifyurl = getString(rs.getString(field.notifyurl.ordinal()+1));
				String returnurl = getString(rs.getString(field.returnurl.ordinal()+1));
				String ip = getString(rs.getString(field.ip.ordinal()+1));
				PayDTO dto = new PayDTO();
				dto.setAppid(appid);
				dto.setSecretkey(secretkey);
				dto.setRequesttoken(requesttoken);
				dto.setBody(body);
				dto.setNotifyurl(notifyurl);
				dto.setReturnurl(returnurl);
				dto.setIp(ip);
				return dto;
			}
		} catch (Exception e) {
			errorSql(e);
			_exHandler.handleQueryException(e, ExceptionHandler.UNKNOWN);
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
			} catch (SQLException e) {
				logError(e);
			}
		}
		return null;
	}

	public boolean empty() throws DataAccessException {
			int result = 0;
			PreparedStatement ps = null;
			String sql = null;
			try {
				_builder = (PreparedStatementBuilder) SqlLoader.getBuilders().get(
						"ADM_PayDAO_EMPTY");
				sql = _builder.getSql(null, null);
				ps = _builder.createStatement(_conn, sql, false, false);
				debugSql();
				result = ps.executeUpdate();

			} catch (Exception e) {
				errorSql(e);
				_exHandler.handleUpdateException(e, ExceptionHandler.UNKNOWN);
			} finally {
				try {
					if (ps != null) {
						ps.close();
					}
				} catch (SQLException e) {
					logError(e);
				}
			}
			if (result == 0) {
				_exHandler.handleUpdateException(null, ExceptionHandler.NOTUPDATED);
			} else if (result > 1) {
				_exHandler.handleUpdateException(null,
						ExceptionHandler.MULTIUPDATED);
			}
			return true;
		}


	@Override
	public boolean insert(PayDTO dto) throws DataAccessException {
		    if(getPay()!=null)
		    	this.empty();
			int result = 0;
			PreparedStatement ps = null;
			String sql = null;
			try {
				_builder = (PreparedStatementBuilder) SqlLoader.getBuilders().get(
						"ADM_PayDAO_INSERT");
				sql = _builder.getSql(null, null);
				ps = _builder.createStatement(_conn, sql, false, false);
				_builder.setParameter(ps, field.secretkey.ordinal()+1, dto.getSecretkey(), Types.VARCHAR);
				_builder.setParameter(ps, field.appid.ordinal()+1, dto.getAppid(), Types.VARCHAR);
				_builder.setParameter(ps, field.requesttoken.ordinal()+1, dto.getRequesttoken(), Types.VARCHAR);
				_builder.setParameter(ps, field.body.ordinal()+1, dto.getBody(), Types.VARCHAR);
				_builder.setParameter(ps, field.notifyurl.ordinal()+1, dto.getNotifyurl(), Types.VARCHAR);
				_builder.setParameter(ps, field.returnurl.ordinal()+1, dto.getReturnurl(), Types.VARCHAR);
				_builder.setParameter(ps, field.ip.ordinal()+1, dto.getIp(), Types.VARCHAR);
				debugSql();
				result = ps.executeUpdate();

			} catch (Exception e) {
				errorSql(e);
				_exHandler.handleUpdateException(e, ExceptionHandler.UNKNOWN);
			} finally {
				try {
					if (ps != null) {
						ps.close();
					}
				} catch (SQLException e) {
					logError(e);
				}
			}
			if (result == 0) {
				_exHandler.handleUpdateException(null, ExceptionHandler.NOTUPDATED);
			} else if (result > 1) {
				_exHandler.handleUpdateException(null,
						ExceptionHandler.MULTIUPDATED);
			}
			return true;
		}
	@Override
	public boolean update(PayDTO dto) throws DataAccessException {
		int result = 0;
		PreparedStatement ps = null;
		String sql = null;
		try {
			_builder = (PreparedStatementBuilder) SqlLoader.getBuilders().get(
					"ADM_PayDAO_UPDATE");
			sql = _builder.getSql(null, null);
			ps = _builder.createStatement(_conn, sql, false, false);
			_builder.setParameter(ps, field.secretkey.ordinal()+1, dto.getSecretkey(), Types.VARCHAR);
			_builder.setParameter(ps, field.appid.ordinal()+1, dto.getAppid(), Types.VARCHAR);
			_builder.setParameter(ps, field.requesttoken.ordinal()+1, dto.getRequesttoken(), Types.DOUBLE);
			_builder.setParameter(ps, field.body.ordinal()+1, dto.getBody(), Types.DOUBLE);
			_builder.setParameter(ps, field.notifyurl.ordinal()+1, dto.getNotifyurl(), Types.DOUBLE);
			_builder.setParameter(ps, field.returnurl.ordinal()+1, dto.getReturnurl(), Types.INTEGER);
			_builder.setParameter(ps, field.ip.ordinal()+1, dto.getIp(), Types.VARCHAR);
			debugSql();
			result = ps.executeUpdate();

		} catch (Exception e) {
			errorSql(e);
			_exHandler.handleUpdateException(e, ExceptionHandler.UNKNOWN);
		} finally {
			try {
				if (ps != null) {
					ps.close();
				}
			} catch (SQLException e) {
				logError(e);
			}
		}
		if (result == 0) {
			_exHandler.handleUpdateException(null, ExceptionHandler.NOTUPDATED);
		} else if (result > 1) {
			_exHandler.handleUpdateException(null,
					ExceptionHandler.MULTIUPDATED);
		}
		return true;
	}

}
